class Walking4 extends egret.Sprite {
    private segment0: Segment
    private segment1: Segment
    private segment2: Segment
    private segment3: Segment
    private offset: number = -Math.PI / 2

    private cycle: number = 0
    public constructor() {
        super()
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this)
    }

    private onAddToStage(event: egret.Event) {
        this.segment0 = new Segment(100, 30)
        this.addChild(this.segment0)
        this.segment0.x = 200
        this.segment0.y = 100

        this.segment1 = new Segment(100, 20)
        this.addChild(this.segment1)
        this.segment1.x = this.segment0.getPin().x
        this.segment1.y = this.segment0.getPin().y

        this.segment2 = new Segment(100, 30)
        this.addChild(this.segment2)
        this.segment2.x = 200
        this.segment2.y = 100

        this.segment3 = new Segment(100, 20)
        this.addChild(this.segment3)
        this.segment3.x = this.segment2.getPin().x
        this.segment3.y = this.segment2.getPin().y

        this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this)
    }

    private onEnterFrame() {
        this.walk(this.segment0, this.segment1, this.cycle);
        this.walk(this.segment2, this.segment3, this.cycle + Math.PI);
        this.cycle += .05;
    }

    private walk(seg0: Segment, seg1: Segment, cyc: number) {
        let angle0: number = Math.sin(cyc) * 45 + 90
        let angle1: number = Math.sin(cyc + this.offset) * 45 + 45
        seg0.rotation = angle0
        seg1.rotation = angle1 + seg0.rotation
        seg1.x = seg0.getPin().x
        seg1.y = seg0.getPin().y
    }
}